package cal;

import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;

public class ArithmeticTest7 {
	Vector<Expression> v;

	public ArithmeticTest7() {
		v = new Vector<Expression>();
	}

	public void produceExpression(int bit, char operator) {
		Expression temp = new Expression();
		temp.operator = operator;
		temp.a = (int) (Math.random() * Math.pow(10, bit));
		temp.b = (int) (Math.random() * Math.pow(10, bit));
		switch (operator) {
		case '+':
			temp.c = temp.a + temp.b;
			break;
		case '-':
			temp.c = temp.a - temp.b;
			break;
		case '*':
			temp.c = temp.a * temp.b;
			break;
		case '/':
			temp.b = (temp.b == 0 ? // 排除除法被除数为0的情况
			temp.b = 1 + (int) (Math.random() * Math.pow(10, bit) - 1)
					: temp.b);
			temp.c = temp.a / temp.b;
			break;
		default:
			mixExpression(temp, bit);// 混合运算
		}

		v.add(temp);
	}

	// 混合运算，产生随机数
	public void mixExpression(Expression temp, int bit) {
		int rand = (int) (Math.random() * 4);
		switch (rand) {
		case 1:
			temp.c = temp.a + temp.b;
			temp.operator = '+';
			break;
		case 2:
			temp.c = temp.a - temp.b;
			temp.operator = '-';
			break;
		case 3:
			temp.c = temp.a * temp.b;
			temp.operator = '*';
			break;
		case 0:
			temp.b = (temp.b == 0 ? // 排除除法被除数为0的情况
			temp.b = 1 + (int) (Math.random() * Math.pow(10, bit) - 1)
					: temp.b);
			temp.c = temp.a / temp.b;
			temp.operator = '/';
			break;
		default:
		}
	}

	public void writeInfo(int num, String userName) {
		File myFile = new File(userName + ".his");
		FileOutputStream fos;
		File saveUserNameAndScore = new File("UserNameAndScore.data");
		try {
			fos = new FileOutputStream(myFile, true);
			PrintStream ps = new PrintStream(fos);
			double score = 0;
			ps.println(String.format("%5s%5s%5s%5s%5s%10s%5s", "", "", "", "",
					"正确答案", "你的答案", "判断"));
			ps.println();
			for (int i = 0; i < num; i++) {

				ps.println(String.format("%5d%5s%5d%5s%10d%10d%10s",
						v.get(i).a, "+", v.get(i).b, "=", v.get(i).c,
						v.get(i).d, (v.get(i).is_right ? "正确" : "错误")));
				ps.println();
				if (v.get(i).is_right) {
					score += 100.0 / num;
				}
			}
			ps.println("你的总分：" + score);
			ps.close();
			fos.close();
			// 下面保存用户名和得分到UserNameAndScore.data文件;
			fos = new FileOutputStream(saveUserNameAndScore, true);
			ps = new PrintStream(fos);
			ps.println(userName);
			ps.println(score);
			ps.close();
			fos.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}

	}

	public static void main(String[] s) {
		OverAllFrame oaf = new OverAllFrame("四则运算");
	}
}
